/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lzh.compiler.parcelerdemo.base;

import com.lzh.compiler.parceler.Parceler;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.utils.PacMap;

import java.util.Map;

/**
 * BaseAbility
 */
public abstract class BaseAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        Parceler.toEntity(this, getIntent());
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        Map<String, Object> maps = outState.getAll();
        IntentParams intentParams = null;
        for (String var : maps.keySet()) {
            intentParams = new IntentParams();
            intentParams.setParam(var, maps.keySet());
        }
        Parceler.toBundle(this, intentParams);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        Map<String, Object> maps = inState.getAll();
        IntentParams intentParams = null;
        for (String var : maps.keySet()) {
            intentParams = new IntentParams();
            intentParams.setParam(var, maps.keySet());
        }
        Parceler.toEntity(this, intentParams);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        Parceler.dispatchActivityResult(this, requestCode, resultCode, resultData);
    }
}
